package domain.regularExpression;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class NonUnaryRegularExpressionOperator extends
		RegularExpressionOperator {

	
	private NonUnaryRegularExpressionOperator(String name, List<RegularExpressionElement> subElements){
		this._name = name;
		this._subElements = subElements;
	}
	
	public static NonUnaryRegularExpressionOperator concatenation(List<RegularExpressionElement> subElements){
		return new NonUnaryRegularExpressionOperator("concatenation",subElements);
	}
	
	public static NonUnaryRegularExpressionOperator pipe(List<RegularExpressionElement> subElements){
		return new NonUnaryRegularExpressionOperator("pipe",subElements);
	}
	
	public static NonUnaryRegularExpressionOperator concatenation(RegularExpressionElement subElem1, RegularExpressionElement subElem2){
		List<RegularExpressionElement> subElements = new Vector<RegularExpressionElement>();
		subElements.add(subElem1);
		subElements.add(subElem2);
		return new NonUnaryRegularExpressionOperator("concatenation",subElements);
	}
	
	public static NonUnaryRegularExpressionOperator pipe(RegularExpressionElement subElem1, RegularExpressionElement subElem2){
		List<RegularExpressionElement> subElements = new Vector<RegularExpressionElement>();
		subElements.add(subElem1);
		subElements.add(subElem2);
		return new NonUnaryRegularExpressionOperator("pipe",subElements);
	}
	
	
	public boolean isConcatenation(){
		return this._name.equals("concatenation");
	}
	
	
	public boolean isPipe(){
		return this._name.equals("pipe");
	}
	
	public String toString(){
		if(isConcatenation()){
			return showConcatOp();
		}else if(isPipe()){
			return showPipeOp(); 
		}else{
			return "unknown operator";
		}
	}
	
	public String showPipeOp(){
		String result = ""; 
		boolean isFirstIt = true;
		Iterator<RegularExpressionElement> itSubElems = _subElements.iterator();
		while(itSubElems.hasNext()){
			if(!isFirstIt){
				result = result + "|";
			}
			result = result + itSubElems.next();
			isFirstIt = false;
		}
		return result;
	}
	
	
	public String showConcatOp(){
		String result = ""; 
		Iterator<RegularExpressionElement> itSubElems = _subElements.iterator();
		while(itSubElems.hasNext()){
			result = result + itSubElems.next();
		}
		return result;
	}
	
}
